//
//  QCHomeOriginalView.swift
//  SwiftDemoWithSina
//
//  Created by qu on 2017/2/5.
//  Copyright © 2017年 Apple. All rights reserved.
//

import UIKit
import SnapKit

class QCHomeOriginalView: UIView {
    //记录原创微博的底部约束
    var orginalViewBottomConstraint: Constraint?
    //提供属性供外界赋值
    var statusViewModel: QCStatusViewModel?{
        
        didSet{
            //赋值
          headerImageView.setImage(urlString: statusViewModel?.homeModel?.user?.profile_image_url)
           nameLabel.text = statusViewModel?.homeModel?.user?.name
            contentLabel.text = statusViewModel?.homeModel?.text
            membershipImageView.image = statusViewModel?.membershipImage
            avatarImageView.image = statusViewModel?.avatarImage
            //卸载约束
            orginalViewBottomConstraint?.deactivate()
            
            if let pictUrls = statusViewModel?.homeModel?.pic_urls, pictUrls.count > 0 {
               
                pictureView.pic_urls = pictUrls
                
                //约束并记录约束
                self.snp.makeConstraints({ (make) in
                orginalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
                })
                //显示配图
                pictureView.isHidden = false
    
            }else {
                self.snp.makeConstraints({ (make) in
                orginalViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(10).constraint
                })
                //隐藏配图
                pictureView.isHidden = true
            }
            
            timeLabel.text = statusViewModel?.sinaTimeStr
            sourceLabel.text = statusViewModel?.sourceStr
        }
    }
    
    
   override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupUI(){
      backgroundColor = UIColor.white
       //添加控件
        addSubview(headerImageView)
        addSubview(nameLabel)
        addSubview(membershipImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(avatarImageView)
        addSubview(contentLabel)
        addSubview(pictureView)
       //添加约束
        headerImageView.snp.makeConstraints { (make) in
            make.left.top.equalToSuperview().offset(10)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        nameLabel.snp.makeConstraints { (make) in
            make.left.equalTo(headerImageView.snp.right).offset(10)
            make.top.equalTo(headerImageView)
        }
        membershipImageView.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabel.snp.right).offset(10)
            make.centerY.equalTo(nameLabel)
        }
        sourceLabel.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabel)
            make.bottom.equalTo(headerImageView)
        }
        timeLabel.snp.makeConstraints { (make) in
            make.left.equalTo(sourceLabel.snp.right).offset(10)
            make.top.equalTo(sourceLabel)
        }
        avatarImageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(headerImageView.snp.right)
            make.centerY.equalTo(headerImageView.snp.bottom)
        }
        contentLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(10)
            make.width.equalTo(KSCREENW - 20)
        make.top.equalTo(headerImageView.snp.bottom).offset(10)
        }
        pictureView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(10)
            make.top.equalTo(contentLabel.snp.bottom).offset(10)
        }
        
        snp.makeConstraints { (make) in
            orginalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
        }
    }
    
    //懒加载
    //头像
    private lazy var headerImageView: UIImageView = UIImageView(imageName: "avatar_default")
    //昵称
    private lazy var nameLabel: UILabel = UILabel(fontSize: QCNORMALFONTSIZE, textColor: UIColor.black)
    //会员等级
    private lazy var membershipImageView: UIImageView = UIImageView(imageName: "common_icon_membership")
    //时间
    private lazy var timeLabel: UILabel = UILabel(fontSize: QCSMALLFONTSIZE, textColor: KCOlOR)
    //来自
    private lazy var sourceLabel: UILabel = UILabel(fontSize: QCSMALLFONTSIZE, textColor: KCOlOR)
    //用户认证
    private lazy var avatarImageView: UIImageView = UIImageView(imageName: "avatar_vgirl")
    //微博正文
    private lazy var contentLabel:UILabel = {
       let lab = UILabel(fontSize: QCNORMALFONTSIZE, textColor: UIColor.black)
        lab.numberOfLines = 0
        return lab
    }()
    //配图
    private lazy var pictureView: QCHomePictureView = {
        let view = QCHomePictureView()
       view.backgroundColor = self.backgroundColor
        return view
        
    }()
}
